

var fs = require('fs');
 var path = require('path');



function duAsync4(dir,resourceMap,lockReads){
  var total = 0;
  var file_counter = 1;// #starts at one because of the initial directory
  var async_running = 0;
  lockReads.v();
  

  function again(current_dir){
      fs.stat(current_dir, function (err, stat) {
          if (err) {
              file_counter--;
              if (file_counter === 0 && async_running === 0) {

                  lockReads.p();
                  //cb(); lockReads.p();
              }
              return;
          }

          if (stat.isFile()) {
              file_counter--;
              total += stat.size;
              lockReads.v();
              var extname = path.extname(current_dir);
              if (extname === ".jss") {
                  fs.readFile(current_dir, "utf-8", function (error, content) {

                      if (error) {
                          //file_counter--;
                      }
                      else {


                          var funcy = new Function("request", "response", content);

                          var str = current_dir.split(":");
                          var str2 = str[0].split(".jss");
                          if (str.length > 1) {
                              str2 = str[1].split(".jss");
                          }



                          var funcKey = str2[0];

                          funcKey = funcKey.slice(dir.length);

                          //because of windows '\\' 
                          funcKey = funcKey.replace(/\\/g, "/");

                          resourceMap[funcKey] = funcy;




                          lockReads.p();

                      }


                  });
              }
          }
          else if (stat.isDirectory()) {
              file_counter--;
              async_running++;

              fs.readdir(current_dir, function (err, files) {
                  async_running--;
                  if (err) {
                      return; // #console.log err.message
                  }
                  file_counter += files.length;

                  for (var i in files) {
                      again(path.join(current_dir, files[i]));
                  }

              });

          }
          else {
              file_counter--;
          }
          if (file_counter === 0 && async_running === 0) {

              lockReads.p();
              //cb(); lockReads.p();
          }
      });

  }


  again(dir);
  
  }

    
function getResourceMap(rootFolder,resourceMap,resourceMapReadyCb) {


   
    var lockReads = new asem(resourceMapReadyCb);
    duAsync4(rootFolder,resourceMap,lockReads);
   
}


exports.getResourceMap=getResourceMap;





function asem(fireFunc, initLock) {
    if (initLock)
        this.lock = initLock;
    else
        this.lock = 0;
    this.func = fireFunc;
}

asem.prototype.v = function () {
    this.lock++;
  
}

asem.prototype.p = function () {
    this.lock--;
  
    if (this.lock == 0 && this.func) {

        this.func();
    }
}

